package com.variometer.models;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.variometer.daos.DaoActualData;
import com.variometer.observer.SynchronizedObservable;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Created by michal.kudlacak on 7/14/13.
 */
public class ActualData extends SynchronizedObservable {

    private List<Location> locations;
    private LocationManager locationManager;
    private Context context;
    private LocationListener locationListener;
    private DaoActualData daoActualData;
    private final AtomicReference<Location> actualLocationRaw;
    private LocationWriter locationWriter;


    public ActualData(long writeLocationDelayMiliseconds, Context context) {
        this.context = context;

        daoActualData = new DaoActualData(context);
        locations = new ArrayList<Location>();
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new MyLocationListener();
        actualLocationRaw = new AtomicReference<Location>();
        actualLocationRaw.set(locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER));
        locationWriter = new LocationWriter(writeLocationDelayMiliseconds);
        new Thread(locationWriter).start();



        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);


    }

    public List<Location> getLocations() {
        return locations;
    }

    public void persist() {
        try {
            daoActualData.SaveToFile("LoggedData.kml", this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stop(){
        locationWriter.setStop(true);
    }
    private class LocationWriter implements Runnable {

        private long writeLocationDelayMiliseconds;
        private boolean stop = false;

        public LocationWriter(long writeLocationDelayMiliseconds) {
            this.writeLocationDelayMiliseconds = writeLocationDelayMiliseconds;
        }

        @Override
        public void run() {
            while (!stop){
                try {
                    Thread.sleep(writeLocationDelayMiliseconds);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Location location = actualLocationRaw.get();
                locations.add(location);
            }


        }

        public void setStop(boolean stop) {
            this.stop = stop;
        }
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            actualLocationRaw.set(location);
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    }
}
